package cn.llming.auth.service.impl;

import cn.llming.auth.domain.SystemPermission;
import cn.llming.auth.mapper.PermissionMapper;
import cn.llming.auth.service.IPermissionScanService;
import cn.llming.auth.util.BasePermission;
import cn.llming.auth.util.PermissionUtil;
import cn.llming.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class PermissionScanServiceImpl extends BaseServiceImpl<SystemPermission> implements IPermissionScanService {



    @Autowired
    private PermissionMapper permissionMapper;


    @Value("${system.controller-scan-package}")
    private String path;


//    @Override
//    public void remveAllPerission() {
//        permissionMapper.remveAllPerission();
//    }

    public void scan(){


        List<Class> allClassName = PermissionUtil.getAllClassName(path);

        List<SystemPermission> systemPermissions=new ArrayList<>();
        for (Class clzz:allClassName) {


            BasePermission annotation = (BasePermission)clzz.getAnnotation(BasePermission.class);
            //筛选与未打注解BasePermission的controller类
            if(Objects.isNull(annotation)){
                continue;
            }
            RequestMapping clazz = (RequestMapping)clzz.getAnnotation(RequestMapping.class);
            //获取RequestMapping上面的访问值
            //将带有BasePermission作为一个父权限
            String s = clazz.value()[0];
            String simpleName = clzz.getSimpleName();

            SystemPermission systemPermission=new SystemPermission();
            //获取BasePermission上面的描述
            systemPermission.setDesc(annotation.desc());

            //获取BasePermission上面的名字
            systemPermission.setName(annotation.name());
            systemPermission.setUrl(s);
            //获取当前类名作为sn
            systemPermission.setSn(simpleName);

            systemPermissions.add(systemPermission);

            //再去寻找所有方法上是否带有BasePermission注解
            Method[] methods = clzz.getMethods();

            for (Method m : methods) {
                BasePermission annotation1 = m.getAnnotation(BasePermission.class);
                if(Objects.isNull(annotation1)){
                    continue;
                }
                //封装方法权限对象
                SystemPermission systemPermission1=new SystemPermission();
                // 获取方法上的属性值
                systemPermission1.setName(annotation1.name());
                systemPermission1.setDesc(annotation1.desc());

                String sn=simpleName+":"+m.getName();
                System.out.println(sn);

                systemPermission1.setSn(sn);
                //设置访问url;
                String methods1 = getMethods(m);
                // System.out.println("测试获取方法方法"+s);
                systemPermission1.setUrl(s+methods1);
                systemPermission1.setParent(systemPermission);

                systemPermissions.add(systemPermission1);


            }
            //删除数据库里所有权限
            permissionMapper.remveAllPerission();

            //向数据库添加权限
            for (SystemPermission systemPermission1: systemPermissions) {
                permissionMapper.insert(systemPermission1);
            }
        }


    }

    public String getMethods(Method m){
        String url="";
        PostMapping post = m.getAnnotation(PostMapping.class);

        if(Objects.nonNull(post)){
            url=post.value() !=null && post.value().length>0?post.value()[0]:"";

        }
        PutMapping put = m.getAnnotation(PutMapping.class);
        if(Objects.nonNull(put)){

            url=put.value() !=null && put.value().length>0?put.value()[0]:"";
        }
        DeleteMapping deleteMapping = m.getAnnotation(DeleteMapping.class);
        if(Objects.nonNull(deleteMapping)){

            url=deleteMapping.value() !=null && deleteMapping.value().length>0?deleteMapping.value()[0]:"";
        }
        GetMapping getMapping = m.getAnnotation(GetMapping.class);
        if(Objects.nonNull(getMapping)){

            url=getMapping.value() !=null && getMapping.value().length>0?getMapping.value()[0]:"";
        }
        GetMapping getMapping1 = m.getAnnotation(GetMapping.class);
        if(Objects.nonNull(getMapping1)){

            url=getMapping1.value() !=null && getMapping1.value().length>0?getMapping1.value()[0]:"";
        }
        PatchMapping patchMapping = m.getAnnotation(PatchMapping.class);
        if(Objects.nonNull(patchMapping)){

            url=patchMapping.value() !=null && patchMapping.value().length>0?patchMapping.value()[0]:"";
        }
        GetMapping getMapping2 = m.getAnnotation(GetMapping.class);
        if(Objects.nonNull(getMapping2)){

            url=getMapping2.value() !=null && getMapping2.value().length>0?getMapping2.value()[0]:"";
        }
        return url;

    }
}
